#!/usr/bin/ksh

################################################################################
#This is a script to find disk hogs in filesystems with high utilization
#This script was written for AIX hosts
#
# Christopher D. DeVille
################################################################################

###SYNTAX function - $0 can be run with size specified in bytes, KB, MB or GB###
SYNTAX() {
print "\n$0 [-f /PATH] [-c] [-e FILENAME.csv] [ -b N | -k N | -m N | -g N ]\n
-c\tLook for files with common suffixes\r
-e\tCreate FILENAME in CSV format to import into Excel spreadsheets\r
-b\tFiles larger than N size in bytes\r
-k\tFiles larger than N size in kilobytes\r
-m\tFiles larger than N size in megabytes\r
-g\tFiles larger than N size in gigabytes\r
-f\tPath to search - ***Default is \"/var, /tmp, /home\"***\n"

exit 1
}

###Are we root?###
if (( $(/usr/bin/id -u) != 0 ))
    then print "\n\t*** You must be root to run this. ***\n " ; 
    SYNTAX ;
fi

###COMMON function - look for files with common suffixes that are good cantidates for deletion###
###JFS & JFS2 Filesystems only###
COMMON() {

find ${FSNAME[*]} \( -fstype jfs -o -fstype jfs2 \) \( -name \*.bak -o -name \*.BAK -o -name \*.log -o -name \*.LOG -o -name \*.tmp -o -name \*.temp -o -name \*.Z -o -name \*.tar -o -name \*.gz -o -name \*.zip -o -name \*.rar \) -ls 2>/dev/null 1>${TEMP} ;

	printf "\n%-13s %-9s %-9s %-13s %-10s\n" SizeInBytes User Group Date Filename ;
	perl -e 'print("-" x 60, "\n" );'
	cat "$TEMP" | awk -F" " '{ printf "%-13s %-9s %-9s %-3s %-2s %-6s %-50s\n", $7, $5, $6, $8, $9, $10, $11}' | sort -nr ;
	if [[ -n "$CSV" ]]
	    then 
	    print "SizeInBytes,User,Group,Date/Time,Filename\n" > $CSV
	    cat "$TEMP" | awk '{print $7","$5","$6","$8, $9, $10","$11}' | sort -nr >> $CSV 
	fi
	exit 0
}

###getopts to parse our arguments
while getopts :b:ce:f:g:k:m:n: args 
do
    case $args in
        b)      BYTES="$OPTARG" ;; 
        c)      COMMON=1 ;; 
        e)      CSV="$OPTARG" ;; 
        f)      set -A FSNAME "$OPTARG" ;; 
        g)      GBYTES="$OPTARG" ;;
        k)      KBYTES="$OPTARG" ;;
        m)      MBYTES="$OPTARG" ;;
        n)      FILE="$OPTARG" ;;
        *)      SYNTAX ;;
    esac
done


###Variable Definitions###
TEMP="/tmp/diskhog.$$"

###Trap on 0,HUP,INT & TERM & cleanup our temp file###
trap '[[ -f "$TEMP" ]] && rm -f ${TEMP}' 0 1 2 15


###If we didn't get passed a filesystem/path name then we'll assume that we want to run against the root filesystem###
if [[ -z "${FSNAME[*]}" ]]
    then set -A FSNAME /var /tmp /home 
fi

if [[ -n "$COMMON" ]]
    then COMMON
fi

###convert our given filesize to bytes if not already in bytes###
if [[ -n "$BYTES" ]]
	then SIZE="$BYTES"
    elif [[ -n "$KBYTES" ]]
	then (( SIZE=KBYTES*1024 ))
    elif [[ -n "$MBYTES" ]]
	then (( SIZE=MBYTES*(1024*1024) ))
    elif [[ -n "$GBYTES" ]]
	then (( SIZE=GBYTES*(1024*1024*1024) ))
	    else 
	    print "\nNo size specified - listing files larger than 5 megabytes\n"
	    SIZE=5242880 
fi

###Find any files in ${FSNAME} array that are $SIZE or larger (assuming that it is a jfs or jfs2 filesystem###
if [[ -z "$FILE" ]]
    then find ${FSNAME[*]} \( -fstype jfs -o -fstype jfs2 \) -size +${SIZE}c -ls 2>/dev/null 1>${TEMP} ;
	else find ${FSNAME[*]} \( -fstype jfs -o -fstype jfs2 \) -size +${SIZE}c -name "${FILE}" -ls 2>/dev/null 1>${TEMP} ;
fi

printf "\n%-13s %-9s %-9s %-13s %-10s\n" SizeInBytes User Group Date Filename ;
perl -e 'print("-" x 60, "\n" );'
cat "$TEMP" | awk -F" " '{ printf "%-13s %-9s %-9s %-3s %-2s %-6s %-50s\n", $7, $5, $6, $8, $9, $10, $11}' | sort -nr ;

if [[ -n "$CSV" ]]
    then 
    print "SizeInBytes,User,Group,Date/Time,Filename\n" > $CSV
    cat "$TEMP" | awk '{print $7","$5","$6","$8, $9, $10","$11}' | sort -nr >> $CSV 
fi

exit 0
		
